package com.finalterm.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.finalterm.order.entity.CompensationTask;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 补偿任务Mapper
 */
@Mapper
public interface CompensationTaskMapper extends BaseMapper<CompensationTask> {
    
    /**
     * 查询需要重试的补偿任务
     */
    @Select("SELECT * FROM compensation_tasks " +
            "WHERE status = 'PENDING' " +
            "AND retry_count < max_retry_count " +
            "AND next_retry_time <= #{currentTime} " +
            "AND deleted = 0 " +
            "ORDER BY create_time ASC " +
            "LIMIT #{limit}")
    List<CompensationTask> selectPendingTasks(@Param("currentTime") LocalDateTime currentTime, 
                                            @Param("limit") int limit);
}
